import os

import numpy as np
import pandas as pd
from PyQt5.QtGui import QFontMetrics
from PyQt5.QtWidgets import (QWidget,
                             QComboBox, QVBoxLayout,
                             QDateEdit, QLabel, QHBoxLayout, QDialog, QFormLayout, QLineEdit, QPushButton)

from src.forward.base.RightWidget import RightWidget
from src.forward.base.bean import forward, MplCanvas
from PyQt5.QtCore import QDate
# current_file_abspath=os.path.abspath(__file__)
# BASE_PATH = os.path.dirname(os.path.dirname(current_file_abspath))
# excel=BASE_PATH+'/backend/sqlite/student.xlsx'

# @forward(name='任务统计')
class Knowledge(RightWidget):

    def __init__(self):
        super().__init__()
        self.knowledge_layout=QVBoxLayout()
        # 查询div
        knowledge_select=QWidget()
        # self.knowledge_table=QTableWidget()
        self.select_layout=QHBoxLayout()
        #学生名
        self.name_label=QLabel('学生名')
        self.name_label.setFixedSize(35,25)
        # self.name_label.setStyleSheet(
        #     "margin-right: 2px;"
        # )
        self.one_level=QComboBox()
        self.one_level.setEditable(True)
        from src.backend.beanfactory import BeanFactory
        df=BeanFactory.getKeyWord().word_df
        alevel=df['one'].drop_duplicates().tolist()
        self.one_level.addItems(alevel)
        self.one_level.lineEdit().setReadOnly(True)
        # 计算最长文本的宽度并设置 QComboBox 的最小宽度
        self.one_level.setMinimumWidth(70)
        self.two_level=QComboBox()
        self.two_level.setFixedWidth(100)
        blevel=df['two'].values.tolist()
        self.two_level.addItems(blevel)
        self.combo_box = QComboBox()
        self.combo_box.setFixedSize(40,25)
        from src.backend.beanfactory import BeanFactory
        sql = 'select * from student'
        data = BeanFactory.getCursor().query(sql)
        self.combo_box.addItems(data['name'].values)
        self.combo_box.currentIndexChanged.connect(self.on_combobox_changed)
        #开始时间
        self.start_label = QLabel('开始日期')
        self.start_label.setFixedSize(50,25)
        self.start_date=QDateEdit()
        self.start_date.setFixedSize(100,25)
        # 设置日期范围
        self.start_date.setMinimumDate(QDate(2023, 1, 1))  # 设置最小日期
        self.start_date.setMaximumDate(QDate(2024, 12, 31))  # 设置最大日期
        self.start_date.setDate(QDate.currentDate())
        self.start_date.setCalendarPopup(True)  # 启用弹出日历
        # 结束时间
        self.end_label = QLabel('结束日期')
        self.end_label.setFixedSize(50,25)
        self.end_date = QDateEdit()
        self.end_date.setFixedSize(100, 25)
        # 设置日期范围
        self.end_date.setMinimumDate(QDate(2023, 1, 1))  # 设置最小日期
        self.end_date.setMaximumDate(QDate(2024, 12, 31))  # 设置最大日期
        self.end_date.setDate(QDate.currentDate())
        self.end_date.setCalendarPopup(True)  # 启用弹出日历

        self.select_layout.addWidget(self.name_label)
        self.select_layout.addWidget(self.combo_box)
        self.select_layout.addWidget(self.one_level)
        self.select_layout.addWidget(self.two_level)
        self.select_layout.addWidget(self.start_label)
        self.select_layout.addWidget(self.start_date)
        self.select_layout.addWidget(self.end_label)
        self.select_layout.addWidget(self.end_date)
        knowledge_select.setLayout(self.select_layout)
        knowledge_select.setStyleSheet("background-color: white;")
        knowledge_select.setFixedSize(500,40)

        self.image=QWidget()
        image_layout=QHBoxLayout()
        self.image.setLayout(image_layout)

        canvas = MplCanvas(self.image, width=5, height=4, dpi=100)
        # 绘制一条正弦曲线
        x = np.linspace(0, 10, 100)
        canvas.axes.plot(x, np.sin(x))
        canvas.axes.set_title("学生任务数量")
        canvas.axes.set_xlabel('学生名')
        canvas.axes.set_ylabel('任务数')
        image_layout.addWidget(canvas)
        self.knowledge_layout.addWidget(knowledge_select)
        self.knowledge_layout.addWidget(self.image)

        self.setLayout(self.knowledge_layout)

    def on_combobox_changed(self):
        item_text = self.combo_box.currentText()
        print(item_text)


    def fill_data(self):

        print('填充完毕')

class AddDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.setWindowTitle('填写信息')
        self.setGeometry(200, 200, 300, 150)
        # 创建表单布局
        layout = QFormLayout()
        # 添加表单字段
        self.name_input = QLineEdit(self)
        self.age_input = QLineEdit(self)
        self.grade_input = QLineEdit(self)

        layout.addRow('姓名:', self.name_input)
        layout.addRow('年龄:', self.age_input)
        layout.addRow('年级:', self.grade_input)

        # 创建确认按钮
        self.submit_button = QPushButton('提交', self)
        self.submit_button.clicked.connect(self.submit_form)

        layout.addWidget(self.submit_button)

        # 设置对话框的布局
        self.setLayout(layout)

    def submit_form(self):
        # 获取输入的值并打印
        from src.backend.entity.manager import Student
        stu = Student()
        stu.name = self.name_input.text()
        stu.age = self.age_input.text()
        stu.grade = self.grade_input.text()
        from src.backend.beanfactory import BeanFactory
        try:
            BeanFactory.getCursor().insert(stu)
        except Exception as e:
            print(e)
        print(f'姓名: {stu.name}, 年龄: {stu.age}，年级:{stu.grade}')
        # 关闭对话框
        self.accept()

